package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CompanyItemActivity extends BasicActivity {
    protected LayoutInflater mInflater;
	@Override
	public String getHeaderTitle() {
		return "公司详情";
	}
	@Override
	public int getContentView() {
		return R.layout.company_item;
	}
	/**
	 * 是否需要检测登录
	 * @return
	 */
	@Override
	public boolean isLoginCheck() {
		return false;
	}
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		String id = (null == bundle) ? "" : bundle.getString("id");
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		if (StringUtils.isBlank(id)) {
			Toast.makeText(this, "缺少请求参数", Toast.LENGTH_SHORT).show();
			this.finish();
			return;
		}
		LinearLayout view = (LinearLayout)findViewById(R.id.company_item_details_zone);
		showProgress();
		this.mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		new InitDataTask(this, view, ContextUtils.getScreenWidth(this)).execute(id);
	}
	
	public class InitDataTask extends AsyncTask<String, Integer, JSONObject> {
		protected Context context;
		protected LinearLayout view;
		protected int imageWidth;
		protected ImageLoader imageLoader = null;
		public InitDataTask(Context context, LinearLayout view, int imageWidth) {
			this.context = context;
			this.imageWidth = imageWidth;
			this.view = view;
			imageLoader = new ImageLoader(context, imageWidth * 2);
		}
		@Override
		protected JSONObject doInBackground(final String... params) {
			if (isCancelled()) {
				return null;
			}
			List<NameValuePair> reqparams = new ArrayList<NameValuePair>();
			reqparams.add(new BasicNameValuePair("companyInfo.id", params[0]));
			return HttpUtils.httpGet(Constants.APP_URL + "/assistant/companyJsonInfo.html", reqparams, 
					new HttpUtils.onGetSuccessListener<JSONObject>() {
						@Override
						public JSONObject onSuccess(int status, String result) {
							if (HttpUtils.SC_OK == status) {
								JSONObject jsonObject = JSONUtils.getJSONObjectFromString(result);
								return JSONUtils.getJSONObjectForKey(jsonObject, "companyInfo");
							} else {
								Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
							}
							return null;
						}
					});
		}
		@Override
		protected void onPostExecute(JSONObject result) {
			super.onPostExecute(result);
			if (null == result) {
				return;
			}
			JSONObject companyInfo = result;
			int childCount = view.getChildCount();
			for (int i = 0 ; i < childCount; i++) {
				View v = view.getChildAt(i);
				String tag = (String)v.getTag();
				if (v instanceof LinearLayout && StringUtils.isNotBlank(tag)) {
					String value = null;
					if (!companyInfo.isNull(tag)) {
						value = JSONUtils.getStringFromJSONObject(companyInfo, tag);
					}
					if (null != value) {
						TextView tv = (TextView)view.findViewById(context.getResources().getIdentifier(tag, "id", context.getPackageName()));
						if (null != tv) {
							tv.setText(value);
						}
					}
				}
			}
			JSONArray ids = JSONUtils.getJSONArrayForKey(companyInfo, "ids");
			if (null != ids && ids.length() > 0) {
				for (int i = 0; i < ids.length(); i++) {
					View mImageView = mInflater.inflate(R.layout.show_image, null);
					ImageView imageView = (ImageView)mImageView.findViewById(R.id.show_image_0);
					imageLoader.displayImage(Constants.APP_URL + "/assistant/image.html?id=" + JSONUtils.getStringFromJSONArray(ids, i), imageView);
					imageView.getLayoutParams().height = imageWidth;
					view.addView(mImageView, childCount + i);
				}
			}
			hideProgress();
		}
	}
}
